package com.mdinfo.gw.door.mapper

import com.baomidou.mybatisplus.mapper.BaseMapper
import com.mdinfo.gw.door.bean.DoorAccess
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.annotations.Select

/**
 * Created by Administrator on 2018/1/8.
 */
interface DoorAccessMapper : BaseMapper<DoorAccess>{
    /**
     * 查询所有门禁
     */
    @Select("""<script>
        select * from door t
        order by t.lastStateTime desc
    </script>""")
    fun listAll(): List<DoorAccess>


    /**
     * 分页条件查询
     */
    @Select("""<script>
        select * from door_access t where 1=1
        <if test="key!=null">
            and (
                t.devId like CONCAT("%",#{key},"%")
                or
                t.name like CONCAT("%",#{key},"%")
                )
        </if>
        <if test="deptId!=null">
            and t.deptId = #{deptId}
        </if>
        <if test="deptIdList!=null and deptIdList.size>0">
            and t.deptId in
            <foreach collection="deptIdList" item="dept_id" open="(" close=")" separator=",">
                #{dept_id}
            </foreach>
        </if>
        order by t.createTime desc
        limit #{start},#{num}
    </script>""")
    fun listQuery(@Param("start") start:Int,
                  @Param("num") num:Int,
                  @Param("key") key:String?,
                  @Param("deptId") deptId:Int?,
                  @Param("deptIdList") deptIdList: List<Int>
    ): List<DoorAccess>


    /**
     * 条件查询总数
     */
    @Select("""<script>
        select count(1) from door_access t where 1=1
        <if test="key!=null">
            and (
                t.devId like CONCAT("%",#{key},"%")
                or
                t.name like CONCAT("%",#{key},"%")
                )
        </if>
        <if test="deptId!=null">
            and t.deptId = #{deptId}
        </if>
        <if test="deptIdList!=null and deptIdList.size>0">
            and t.deptId in
            <foreach collection="deptIdList" item="dept_id" open="(" close=")" separator=",">
                #{dept_id}
            </foreach>
        </if>
    </script>""")
    fun countListQuery(@Param("key") key:String?,
                       @Param("deptId") deptId:Int?,
                       @Param("deptIdList") deptIdList: List<Int>
    ): Int

    /**
     * 根据设备号查询
     */
    @Select("""<script>
        select * from door_access t
        where t.devId = #{devId}
    </script>""")
    fun getByDevId(devId:String):DoorAccess?


    /**
     * 获取本部门下的门禁控制器
     */
    @Select("""<script>
        select * from door_access t
        where t.deptId = #{deptId}
    </script>""")
    fun getBydept(deptId:Int):List<DoorAccess>


    /**
     * 获取所有可用的门禁控制器
     */
    @Select("""<script>
        select * from door_access t
        where t.onLine = 1
    </script>""")
    fun listAllUsable():List<DoorAccess>

}